import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import NotFound from '../views/NotFound.vue'
import store from '@/store'
Vue.use(VueRouter)
     const one = {
       path: '/menu/one',
       component: () => import('@/views/Page1.vue')
      }
      const two = {
        path: '/menu/two',
        component: () => import('@/views/Page1.vue')
      }
      const three = {
        path: '/menu/three',
        component: () => import('@/views/Page1.vue')
      }
      const four = {
        path: '/menu/four',
        component: () => import('@/views/Page1.vue')
      }
      const five = {
        path: '/menu/five',
        component: () => import('@/views/Page1.vue')
      }
const ruleMapping = {
  '/menu/one':one,
  '/menu/two':two,
  '/menu/three':three,
  '/menu/four':four,
  '/menu/five':five
}
/* const map = {
  '/menu/one': { path: 'menu/one', component: () => import('@/views/Page1.vue')},
  '/menu/two': { path: 'menu/two', component: () => import('@/views/Page1.vue') },
  '/menu/three': { path: 'menu/three', component: () => import('@/views/Page1.vue') },
  '/menu/four': { path: 'menu/four', component: () => import('@/views/Page1.vue') },
  '/menu/five': { path: 'menu/five', component: () => import('@/views/Page1.vue') }
} */
const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/menu/one',
    children: []
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '*',
    name: 'NotFound',
    component: NotFound
  }
]

const router = new VueRouter({
  routes,
  mode:'history'
})
//路由前置导航守卫
router.beforeEach((to,from,next)=>{
  if(to.path == '/login'){
    next()
  }else {
    let token = window.sessionStorage.getItem('token')
    if(!token){
      next('/login')
    }else {
      next()
    }
  }
})
export function initDynamicRoutes(){
  console.log(router);
  let currentrouters = router.options.routes
  let rightsList = store.state.user.rights
  console.log(rightsList);
  if(rightsList) {
    rightsList.forEach(item=>{
      console.log(item);
      item.children.forEach(child=>{
        console.log(child);
        if(child.path){
          const temp = ruleMapping[child.path]
          currentrouters[0].children.push(temp)
        }
      })
    })
  }
  router.addRoutes(currentrouters)
}
/* export function initDynamicRoutes(){
   var currentrouters = router.options.routes
   console.log(router);
   let rightsList = store.state.user.rights
   if(rightsList){
     rightsList.forEach(item=>{
       item.children.forEach(child=>{
         console.log(child);
         if(child.path){
           const temp = ruleMapping[child.path]
           console.log(temp);
           currentrouters[0].children.push(temp)
         }
       })
     })
   } */
/* const currentrouters = router.options.routes
 console.log(currentrouters);
  const rights = store.state.user.rights
   console.log(rights);
    if(rights){
     rights.forEach(function(item){
       if(item.children){
         item.children.forEach(function(item){
          console.log(item);
           const temp = map[item.path]
           console.log(temp)
           console.log(currentrouters[0].children)
           currentrouters[0].children.push(temp)
         })
       }
     })
    } */
/* router.addRoutes(currentrouters)
} */
const originalPush = VueRouter.prototype.push
// 重写了原型上的push方法，统一的处理了错误信息
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

export default router
